package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Button;
import com.codename1.ui.CN;
import com.codename1.ui.CommonProgressAnimations.CircleProgress;
import static com.codename1.ui.ComponentSelector.$;
import com.codename1.ui.animations.CommonTransitions;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.FlowLayout;
import com.codename1.util.AsyncResource;
import java.util.Timer;
import java.util.TimerTask;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class CircleProgressSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        Button b = new Button("Show Details");
        b.addActionListener(e->{
            showForm();
        });
        hi.add(b);
        hi.show();
    }
    
    private void showForm() {
        Form f = new Form("Hello", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
        Form prev = CN.getCurrentForm();
        Toolbar tb = new Toolbar();
        f.setToolbar(tb);
        tb.addCommandToLeftBar("Back", null, evt->{
            prev.showBack();
        });
        Label nameLabel = new Label();
        $(nameLabel).setAlignment(CENTER);
        nameLabel.setText("placeholder");
        f.add(BorderLayout.CENTER, nameLabel);
        // Replace the label by a CircleProgress to indicate that it is loading.
        CircleProgress.markComponentLoading(nameLabel)
                .getStyle().setFgColor(0xff0000);

        AsyncResource<MyData> request = fetchDataAsync();
        request.ready(data -> {
            nameLabel.setText(data.getName());

            // Replace the progress with the nameLabel now that
            // it is ready, using a fade transition
            CircleProgress.markComponentReady(nameLabel, CommonTransitions.createFade(300));
        });
        
        f.show();

    }
    
    private class MyData {
        String getName() {
            return "Steve";
        }
    }
    
    private AsyncResource<MyData> fetchDataAsync() {
        final AsyncResource<MyData> out = new AsyncResource<>();
        Timer t = new Timer();
        t.schedule(new TimerTask() {
            
            @Override
            public void run() {
                out.complete(new MyData());
            }
            
        }, 2000);
        
        return out;
    }


    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
